//
//  PCCenterLabelAlert.m
//  U9WatchGame
//
//  Created by u9-baken on 2019/4/18.
//  Copyright © 2019 UUU9. All rights reserved.
//

#import "PCCenterLabelAlert.h"

static PCCenterLabelAlert *_centerAlert;
@interface PCCenterLabelAlert()

@property (nonatomic, strong) UILabel *label;

@end

@implementation PCCenterLabelAlert

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)sharedCenterAlert{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _centerAlert = [[PCCenterLabelAlert alloc] init];
    });
    return _centerAlert;
}

-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 20;
        self.hidden = YES;
        [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo([[[UIApplication sharedApplication] delegate] window]);
            make.size.mas_equalTo(CGSizeMake(100, 40));
        }];
        [self addSubViews];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissAlert)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}


- (void)addSubViews{
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    _label = label;
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
         make.size.mas_equalTo(CGSizeMake(80, 40));
    }];
    
}

- (void)setTitle:(NSString *)title{
    _title = title;
    _label.text = title;
}

- (void)showAlert:(AlertBlock)block{
    [UIView animateWithDuration:0.15 animations:^{
        self.hidden = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissAlert:block];
        });
    }];
}


- (void)dismissAlert:(AlertBlock)block{
    [UIView animateWithDuration:0.15 animations:^{
        block();
        [self dismissAlert];
    }];
}

- (void)dismissAlert{
    self.hidden = YES;
}


@end
